package com.hsnn.medstgmini.trading.catalog.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.exception.MSException;
import com.hsnn.medstgmini.pro.base.model.BaseGoods;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.trading.catalog.dao.PurHospCatalogDao;
import com.hsnn.medstgmini.trading.catalog.model.PurHospcatalog;
import com.hsnn.medstgmini.trading.catalog.model.form.GoodsSelectForm;
import com.hsnn.medstgmini.trading.catalog.service.PurHospCatalogManager;
import com.hsnn.medstgmini.util.DicSysUtil;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseNumber;
@Service("bidProjCompanyManager")
public class PurHospCatalogManagerImpl implements PurHospCatalogManager {
	private static final Logger logger = LoggerFactory.getLogger(PurHospCatalogManagerImpl.class);
	@Autowired
	private PurHospCatalogDao purHospCatalogDao;
	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;
	
	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getGoodsSelectList(Pagination page) {
		page = purHospCatalogDao.getGoodsSelectList(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page.getRows();
		if (null != list && list.size() > 0){
			for (int i = 0; i < list.size(); i++){
				Map<String, Object> map = list.get(i);
				if (map.get("goodsSource") != null){
					if (ParseNumber.toInteger(map.get("goodsSource").toString()) == Constants.GOODS_SOURCE_BACG){
						map.put("bakFlag", true);
					}
					map.put("goodsSource", DicSysUtil.getValueByClassCodeAndDicCode(Constants.PRO_METHOD, map.get("goodsSource").toString()));
				}
				
			}
		}
		return page;
	}
	
	public Pagination getDelCompList(Pagination page){
		page = purHospCatalogDao.getDelCompList(page);
		return page;
	}
	@Transactional
	public Pagination saveGoodsSelect(Pagination page){
		return purHospCatalogDao.saveGoodsSelect(page);
	}
	@Transactional
	public void batchSaveGoodsSelect(List<GoodsSelectForm> list){
		try {
			getSession().getTransaction().begin();
			purHospCatalogDao.batchSaveGoodsSelect(list);
			getSession().getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("批量勾选目录：", e);
			getSession().getTransaction().rollback();
			throw new MSException(e.getMessage());
		}
		
	}
	/**
	 * 
	 *@category 医疗机构获取已勾选目录
	 *@author 邱磊
	 *@date 2015年7月7日 上午10:15:02
	 *@param page
	 *@return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getHaveSelectData(Pagination page){
		page = purHospCatalogDao.getHaveSelectData(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page.getRows();
		if (null != list && list.size() > 0){
			for (int i = 0; i < list.size(); i++){
				Map<String, Object> map = list.get(i);
				map.put("setDel", true);
				if (null != map.get("goodsSource")){
					if (ParseNumber.toInteger(map.get("goodsSource").toString()) == Constants.GOODS_SOURCE_BACG){
						map.put("setDel", false);
					}
					map.put("proPrice", map.get("proPrice")==null?"":StringUtils.isNotBlank(map.get("proPrice").toString())?ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("proPrice").toString())):"");
					map.put("proPriceLimit", map.get("proPriceLimit")==null?"":StringUtils.isNotBlank(map.get("proPriceLimit").toString())?ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("proPriceLimit").toString())):"");
					map.put("retailPriceLimit", map.get("retailPriceLimit")==null?"":StringUtils.isNotBlank(map.get("retailPriceLimit").toString())?ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("retailPriceLimit").toString())):"");
					map.put("goodsSource", DicSysUtil.getValueByClassCodeAndDicCode(Constants.PRO_METHOD, map.get("goodsSource").toString()));
				} 
			}
		}
		return page;
	}
	
	/**
	 * 
	 *@category 修改采购价
	 *@author 邱磊
	 *@date 2015年7月7日 上午10:40:46
	 *@param form
	 */
	public void modifyPrice(GoodsSelectForm form){
		purHospCatalogDao.modifyPrice(form);
	}
	public void batchSwitchStatus(List<GoodsSelectForm> list){
		purHospCatalogDao.batchSwitchStatus(list);
	}
	public void batchModifyData(List<GoodsSelectForm> list, SysUser user){
		purHospCatalogDao.batchModifyData(list,user);
	}
	

	public void save(PurHospcatalog data) {
		purHospCatalogDao.save(data);
	}
	public BaseGoods getBaseGoddsByCode(String goodsCode){
		return purHospCatalogDao.getBaseGoddsByCode(goodsCode);
	}


	@Override
	public double getProPriceLimitByGoodsCode(String code) {
		return purHospCatalogDao.getProPriceLimitByGoodsCode(code);
	}

	@Override
	public Pagination getDelCompListWithGoods(Pagination page) {
		return purHospCatalogDao.getDelCompListWithGoods(page);
	}
	@Override
	public Pagination getDelCompListWithoutGoods(Pagination page) {
		return purHospCatalogDao.getDelCompListWithoutGoods(page);
	}
	
	/**
	 * @category 根据商品编码获取所有配送企业
	 * @author 韩守松
	 * @date   2015年12月8日
	 * @param  @param page
	 * @param  @return
	 */
	public Pagination getAllDelByGoodsCode(Pagination page){
		return purHospCatalogDao.getAllDelByGoodsCode(page);
	}
	
	/**
	 * @category 保存新增的配送关系
	 * @author 韩守松
	 * @date   2015年12月8日
	 * @param  @param list
	 * @param  @param user
	 */
	public void goodsWithDelSave(List<GoodsSelectForm> list, SysUser user){
		purHospCatalogDao.goodsWithDelSave(list, user);
	}
	
	/**
	 * @category 批量修改采购目录表配送企业
	 * @author lil
	 * @date   2015年12月13日
	 * @param  List<GoodsSelectForm> list
	 * @param  SysUser user
	 * @param  @return
	 */
	public void batchModifyCgmlZx(List<GoodsSelectForm> list, SysUser user){
		purHospCatalogDao.batchModifyCgmlZx(list,user);
	}
	
	/**
	 * @category 根据主键编号删除采购目录数据
	 * @author lil
	 * @date   2015年12月18日
	 * @param  List<GoodsSelectForm> list
	 * @param  @return
	 */
	public void deleteHospcatalog(List<GoodsSelectForm> list){
		purHospCatalogDao.deleteHospcatalog(list);
	}
	
}